function GetDescByLangCode(oObjs)
{
var iLen = oObjs.length;

for (var i = 0; i < iLen; i++)
{
if (oObjs[i].LanguageCode == USER_LANGUAGE_CODE)
{
return oObjs[i].Description;
}
}

return "";
}


function GetLocalizedObjsArray(oNodes)
{
var oObjs = new Array();
var oNode;
var iLen = oNodes.length;

for (var i = 0; i < iLen; i++)
{
oNode = oNodes[i];
oObjs[i] =	new LocalizedObj(oNode.getAttribute("description"), oNode.getAttribute("languagecode"));
}

return oObjs;
}


function GetGridRowNode(oGridXml)
{
return oGridXml.selectSingleNode("//row");
}


function GetGridCellNode(oGridXml, sFieldName)
{
return oGridXml.selectSingleNode("/grid/row/cell[@name = '" + sFieldName + "']");
}

function GetFieldDisplayName(oPropertiesXml, sEntityName, sFieldName)
{
var oFieldNode = oPropertiesXml.selectSingleNode("entities/entity[@name=\"" + sEntityName + "\"]/fields/field[@name = '" + sFieldName + "']");
return oFieldNode!=null ? GetDescByLangCode(GetLocalizedObjsArray(oFieldNode.selectNodes("displaynames/displayname"))) : null;
}


function GetFetchEntityNode(oFetchXml)
{
return oFetchXml.selectSingleNode("//entity");
}


function GetFetchAttributeNode(oFetchXml, sFieldName)
{

var oFieldName = GetFieldAliasName(sFieldName);
if (oFieldName.Alias.length == 0)
{
return oFetchXml.selectSingleNode("/fetch/entity/attribute[@name = '" + sFieldName + "']");
}
else
{

return oFetchXml.selectSingleNode("/fetch/entity/link-entity[@alias=\"" + oFieldName.Alias + "\"]/attribute[@name = '" + oFieldName.FieldName + "']");
}
}

function isSortableField(oFieldsXml, sEntityName, sFieldName)
{

if (sFieldName.indexOf(".") != -1)
{
return false;
}

var oFieldNode = oFieldsXml.selectSingleNode("entities/entity[@name=\"" + sEntityName + "\"]/fields/field[@name = \"" + CrmEncodeDecode.CrmXmlEncode(sFieldName) + "\"]");
if (oFieldNode == null) return false;
var sDataType = oFieldNode.getAttribute("datatype").text;



switch (sDataType)
{
case "partylist":
case "memo":
return false;

default:
return true;
}
}


function GetFieldInfo(sFieldName)
{
var oFieldName = GetFieldAliasName(sFieldName);
var sAlias = oFieldName.Alias;
sFieldName = oFieldName.FieldName;

var sEntityName="", sEntityDisplayName="", sType=null, sSchemaName=null, sDisplayName=null, sGridFieldDisplayMask="{0}";
if (sAlias.length == 0)
{
sEntityName = _sPrimaryEntityName;
sEntityDisplayName = _sPrimaryEntityDisplayName;
}
else
{
var oEntity = oEntitiesXml.selectSingleNode("entities/entity[@alias=\"" + sAlias + "\"]");
if (oEntity != null)
{
sEntityName = oEntity.getAttribute("name");
sGridFieldDisplayMask = oEntity.getAttribute("griddisplaynamemask");
sEntityDisplayName = oEntity.getAttribute("entitydisplayname");
}
}

var oFieldNode = null;
if (sEntityName.length > 0)
{
var oFieldNode = oFieldsXml.selectSingleNode("entities/entity[@name=\"" + sEntityName + "\"]/fields/field[@name = '" + sFieldName + "']");
if (oFieldNode != null)
{
sDisplayName = GetFieldDisplayName(oPropertiesXml, sEntityName, sFieldName);
sType = oFieldNode.getAttribute("datatype");
sSchemaName	= oFieldNode.getAttribute("name");
}
}

return {Alias: sAlias, FieldName : sFieldName, EntityName : sEntityName, EntityDisplayName : sEntityDisplayName, Type : sType, DisplayName : sDisplayName, SchemaName : sSchemaName, GridFieldDisplayMask : sGridFieldDisplayMask, InvalidField : oFieldNode==null};
}


function GetFieldAliasName(sFieldName)
{
var iPos = sFieldName.indexOf(".");
var sAlias="";
if (iPos != -1)
{
sAlias = sFieldName.substr(0, iPos);
sFieldName = sFieldName.substr(iPos+1);
}

return {Alias: sAlias, FieldName : sFieldName};
}



function GetFieldsXml(sEntityName, sAlias)
{
if (typeof(sEntityName) == "undefined")
{
var sEntityName = _sPrimaryEntityName;
}

return GetFieldsXmlInternal(sEntityName, sAlias, _oGridXml, _bQFAddSearch, _bExportSelect, _bUpdateOnAttr, _bSelectMode);
}


function GetFieldsXmlInternal(sEntityName, sAlias, oGridXml, bQFAddSearch, bExportSelect, bUpdateOnAttr, bSelectMode)
{
var oFieldsXml;
var oPropertiesXml;


oFieldsXml		= _oAllFieldsXml.selectSingleNode("/entities/entity[@name=\"" + sEntityName + "\"]");
oPropertiesXml	= _oAllFieldPropertiesXml.selectSingleNode("/entities/entity[@name=\"" + sEntityName + "\"]");

if (IsNull(oFieldsXml) || IsNull(oPropertiesXml))
{

var oRemoteCmd = new RemoteCommand("AdvancedFind", "GetFieldsPropertiesXml");
oRemoteCmd.SetParameter("entityName", sEntityName);
var oResult = oRemoteCmd.Execute();
if (oResult.Success != ERROR_NONE)
{
return null;
}
oFieldsXml = CreateXmlDocument();
oFieldsXml.loadXML(oResult.ReturnValue.FieldXml);
oFieldsXml = _oAllFieldsXml.documentElement.appendChild(oFieldsXml.documentElement);

oPropertiesXml = CreateXmlDocument();
oPropertiesXml.loadXML(oResult.ReturnValue.PropertiesXml);
oPropertiesXml = _oAllFieldPropertiesXml.documentElement.appendChild(oPropertiesXml.documentElement);
}

var sFieldName;
var sFilter = "";


if (!(bQFAddSearch || bExportSelect || bUpdateOnAttr || bSelectMode))
{
sFilter = BuildUsedFieldFilter(oGridXml.selectNodes("/grid/row/cell"), sEntityName, sAlias);
}


var oFields = oPropertiesXml.selectNodes("fields/field" + ((sFilter.length > 0) ? "[" + sFilter + "]" : ""));


var sFieldsXml = "<fields>";

var i;
var iLen = oFields.length;

for (i = 0; i < iLen; i++)
{
sFieldName = oFields[i].getAttribute("name");
var oEntityFieldNode = oFieldsXml.selectSingleNode("fields/field[@name = '" + CrmEncodeDecode.CrmXmlEncode(sFieldName) + "']");
if (oEntityFieldNode != null)
{
var bValidForAdvancedFind = oEntityFieldNode.attributes.getNamedItem("validforadvancedfind").text == "true";
var bValidForGrid = oEntityFieldNode.attributes.getNamedItem("validforgrid").text == "true";
var sDataType = oEntityFieldNode.attributes.getNamedItem("datatype").text;

if (sFieldName != "" && ((bQFAddSearch && bValidForAdvancedFind && IsValidForQuickFind(sDataType)) ||(!bQFAddSearch && bValidForGrid)))
{
var displayNameObject = oFields[i].selectSingleNode("displaynames/displayname[@languagecode = " + USER_LANGUAGE_CODE + "]/@description");



if (!IsNull(displayNameObject))
{
sFieldsXml += "<field name=\"" + CrmEncodeDecode.CrmXmlEncode(sFieldName) +
"\" displayname=\"" + CrmEncodeDecode.CrmXmlEncode(displayNameObject.text) +
"\" datatype=\"" + sDataType +
"\" required=\"false" +
"\" orgrequired=\"" + oPropertiesXml.selectSingleNode("fields/field[@name = \"" + CrmEncodeDecode.CrmXmlEncode(sFieldName) + "\"]/@requiredlevel").text +
"\"/>";
}
}
}
}

return sFieldsXml + "</fields>";
}

function BuildUsedFieldFilter(oNodes, sEntityName, sAlias)
{
var sFilter = "";

var i, iPos, sFieldAlias, sFieldEntity;
var iLen = oNodes.length;
var asAliasEntityMap = new Array();

for (i = 0; i < iLen; i++)
{
var sFieldName = oNodes[i].getAttribute("name");

if ((iPos = sFieldName.indexOf(".")) != -1)
{
sFieldAlias = sFieldName.substring(0, iPos);
sFieldEntity = asAliasEntityMap[sFieldAlias];
if (sFieldEntity == null)
{
var oEntity = _oEntitiesXml.selectSingleNode("entities/entity[@alias=\"" + sFieldAlias + "\"]");
if (oEntity != null)
{
asAliasEntityMap[sFieldAlias] = sFieldEntity = oEntity.getAttribute("name");
}
}
}
else
{
sFieldEntity = _sPrimaryEntityName;
}

if (sFieldEntity == sEntityName && sFieldAlias == sAlias)
{

sFieldName = sFieldName.substr(iPos+1);
if (sFilter.length == 0)
{
sFilter += "@name != '" + sFieldName + "'";
}
else
{
sFilter += " and @name != '" + sFieldName + "'";
}
}
}

return sFilter;
}

function IsValidForQuickFind(sDataType)
{
var bValid = false;


switch(sDataType)
{
case "text":
case "memo":
case "boolean":
case "lookup":
case "owner":
case "customer":
case "picklist":
case "state":
case "status":
case "integer":
case "money":
case "datetime":
case "float":
bValid = true;
break;
}

return bValid;
}
